<template>
  <div class='container'>
    <el-dialog
      :title="titleName"
      :visible="showDialog"
      width="800px"
      @close="btnCancel"
    >
      <el-form
        ref="articleForm"
        :model="formData"
        :rules="rules"
        label-width="120px"
        size="mini"
      >
        <el-form-item
          label="文章标题"
          prop="title"
        >
          <el-input
            v-model="formData.title"
            style="width: 80%"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="文章内容"
          prop="articleBody"
        >
          <!-- 富文本组件 -->
          <quill-editor
            v-model="formData.articleBody"
            style="width: 80%"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input
            v-model="formData.videoURL"
            style="width: 80%"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row
        slot="footer"
        type="flex"
        justify="center"
      >
        <el-col :span="6">
          <el-button
            type="primary"
            size="small"
            @click="btnOk"
          >确定</el-button>
          <el-button
            size="small"
            @click="btnCancel"
          >取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, detail, update } from '@/api/hmmm/articles.js'
export default {
  components: {
    quillEditor
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formData: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
        articleBody: [{ required: true, message: '文章内容不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    titleName () {
      return this.formData.id ? '编辑文章' : '新增文章'
    }
  },
  methods: {
    async detail (id) {
      const { data } = await detail({ id: id })
      this.formData = data
    },
    btnOk () {
      this.$refs.articleForm.validate(async () => {
        try {
          if (this.formData.id) {
            await update(this.formData)
            this.$message.success('修改文章成功')
          } else {
            await add(this.formData)
            this.$message.success('添加文章成功')
          }
          this.$emit('addArticle')
          this.$emit('update:showDialog', false)
        } catch (error) {
          console.log(error)
        }
      })
    },
    btnCancel () {
      this.formData = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.articleForm.resetFields()
    }
  }
}
</script>

<style scoped lang='scss'></style>
